# -*-coding:Utf-8 -*
""" UI Zone Class
"""
import bge, bgl, blf
from ..common import *
from .color import *

class Zone(object):
	
	def __init__(self, parent,x, y, width, height):
		self._parent = None
		self.visible = True
		self._visibility = True
		self.childs = ChildList(self)
		self.parent = parent
		self._x = 0
		self._y = 0
		self._w = 0
		self._h = 0
		self.relative = [None,None,None,None]
		self.anim = [None,None,None,None,None]
		self._hover = False
		self.color = None
		self.x = x
		self.y = y 
		self.w = width
		self.h = height 
		self._onclic = Zone._defaultonclic
		self._onrightclic = Zone._defaultonclic
		self._onwheel = Zone._defaultonclic
		self._onwheelup = Zone._defaultonclic
		self._onwheeldown = Zone._defaultonclic
		
	@property
	def parent(self):
		return self._parent
	@parent.setter
	def parent(self, value):
		if self._parent != None:
			self._parent.delChild(self)
		if hasattr(value,"setHover") and callable(value.setHover):
			value.addChild(self)
			self._parent=value
		elif value == None:
			self._parent = None
	
	def addChild(self,value):
		self.childs._list.append(value)
		
	def delChild(self,value):
		if value in self.childs._list:
			self.childs._list.remove(value)
			
	def destroy(self):
		self.parent = None
		self.color = None
		
	def set_size_x(self,value,ratio):
		if self.relative[0] != None:
			self.x = self.relative[0]
		if self.relative[2] != None:
			self.w = self.relative[2]
		self.check_visibility()
			
	def set_size_y(self,value,ratio):
		if self.relative[1] != None:
			self.y = self.relative[1]
		if self.relative[3] != None:
			self.h = self.relative[3]
		self.check_visibility()
			
	def check_visibility(self):
		self._visibility = True
		if .0 in (self._w,self._h):
			self._visibility = False
		if not hasattr(self.parent,"parent") and (self._x > self.parent.w or self._y > self.parent.h):
			self._visibility = False
		
	@property
	def x(self):
		if self.anim[0] != None:
			return round(self.anim[0].value)
		return self._x
			
	@x.setter	
	def x(self, value):
		self._setx(value)
		if self.anim[0] != None:
			self.anim[0].value = self._x
	
	def _setx(self, value):
		if type(value) == float and .0 < value <= 1.0:
			self.relative[0] = value
			value = round(self.parent.w*value)
		elif type(value) == float and -1.0 <= value < .0:
			self.relative[0] = value
			value = round(self.parent.w + (self.parent.w*value))
		elif type(value) in (int,float) and value < 0:
			self.relative[0] = value
			value = round(self.parent.w + value)
		elif type(value) == str and value != "" and value[0] == 'i':
			self.relative[0] = value
			value = value[1:]
			if 'i' in value:
				self.relative[0] = None
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and .0 < value <= 1.0:
				value = round((self.parent.w - (self.parent.w*value))-self.w)
			elif type(value) == float and -1.0 <= value < .0:
				value = round((self.parent.w*(-value))-self.w)
			elif value < 0:
				value = round(abs(value)-self.w)
			else:
				value = round(self.parent.w-(value+self.w))
		elif type(value) == str and value != "" and value[0] == 'c':
			self.relative[0] = value
			value = value[1:]
			if 'c' in value:
				self.relative[0] = None
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and -1.0 < value <= 1.0:
				value = round(((self.parent.w/2)-(self.w/2))+(value*(self.parent.w/2)))
			else:
				value = round((self.parent.w/2)-(self.w/2))+round(value)
		else:
			self.relative[0] = None
			value = round(value)
		self._x = int(value)
		self.check_visibility()
	
	@property
	def y(self):
		if self.anim[1] != None:
			return round(self.anim[1].value)
		return self._y
	@y.setter	
	def y(self, value):
		self._sety(value)
		if self.anim[1] != None:
			self.anim[1].value = self._y
	
	def _sety(self, value):
		if type(value) == float and .0 < value <= 1.0:
			self.relative[1] = value
			value = round(self.parent.h*value)
		elif type(value) == float and -1.0 <= value < .0:
			self.relative[1] = value
			value = round(self.parent.h + (self.parent.h*value))
		elif type(value) in (int,float) and value < 0:
			self.relative[1] = value
			value = round(self.parent.h + value)
		elif type(value) == str and value != "" and value[0] == 'i':
			self.relative[1] = value
			value = value[1:]
			if 'i' in value:
				self.relative[1] = None
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and .0 < value <= 1.0:
				value = round((self.parent.h - (self.parent.h*value))-self.h)
			elif type(value) == float and -1.0 <= value < .0:
				value = round((self.parent.h*(-value))-self.h)
			elif value < 0:
				value = round(abs(value)-self.h)
			else:
				value = round(self.parent.h-(value+self.h))
		elif type(value) == str and value != "" and value[0] == 'c':
			self.relative[1] = value
			value = value[1:]
			if 'c' in value:
				self.relative[1] = None
				return
			if '.' in value:
				value=float(value)
			else:
				value = int(value)
			if type(value) == float and -1.0 < value <= 1.0:
				value = round(((self.parent.h/2)-(self.h/2))+(value*(self.parent.h/2)))
			else:
				value = round((self.parent.h/2)-(self.h/2))+round(value)
		else:
			self.relative[1] = None
			value = round(value)
		self._y = int(value)
		self.check_visibility()
			
	@property
	def w(self):
		if self.anim[2] != None:
			return round(self.anim[2].value)
		return self._w
	@w.setter	
	def w(self, value):
		self._setw(value)
		if self.anim[2] != None:
			self.anim[2].value = self._w
		
	
	def _setw(self, value):
		if type(value) in (int,float) and value < 0:
			value = -value
		if type(value) == float and .0 <= value <= 1.0:
			self.relative[2] = value
			value = round(self.parent.w*value)
		else:
			self.relative[2] = None
			value = round(value)
		tmp = 0
		if not self._w == 0:
			tmp =  value/self._w
		self._w = int(value)
		if self.relative[0] != None and ((type(self.relative[0]) == str and self.relative[0] != "" and self.relative[0][0] in 'ic') or (type(self.relative[0]) != str and self.relative[0] < 0)):
			self.x = self.relative[0]
		else:
			self.check_visibility()
		for aChild in self.childs:
			aChild.set_size_x(value,tmp)
	
	@property
	def h(self):
		if self.anim[3] != None:
			return round(self.anim[3].value)
		return self._h
	@h.setter	
	def h(self, value):
		self._seth(value)
		if self.anim[3] != None:
			self.anim[3].value = self._h
	
	def _seth(self, value):
		if type(value) in (int,float) and value < 0:
			value = -value
		if type(value) == float and .0 < value <= 1.0:
			self.relative[3] = value
			value = round(self.parent.h*value)
		else:
			self.relative[3] = None
			value = round(value)
		tmp = 0
		if not self._h == 0:
			tmp =  value/self._h
		self._h = int(value)
		if self.relative[1] != None and ((type(self.relative[1]) == str and self.relative[1] != "" and self.relative[1][0] in 'ic') or (type(self.relative[1]) != str and self.relative[1] < 0)):
			self.y = self.relative[1]
		else:
			self.check_visibility()
		for aChild in self.childs:
			aChild.set_size_y(value,tmp)
			
	def hide(self, duration = 0):
		if duration > 0:
			Animate(self.anim,4,0.9999,0.0,duration)
		else:
			self.visible = False
	def show(self, duration = 0):
		self.visible = True
		if duration > 0:
			Animate(self.anim,4,0.00001,1.0,duration)
			
	def getAlpha(self):
		alpha = 1.0
		alpha *= self.parent.getAlpha()
		if self.anim[4] != None:
			alpha *= self.anim[4]
		return alpha
			
	@property
	def hover(self):
		return self._hover
			
	def isHover(self, mousex,mousey, x,y,hover):
		if self.visible and x+self.x < mousex < x+self.x+self.w and y+self.y < mousey < y+self.y+self.h:
			hover = self
		self._hover = False
		if self.visible:
			for aChild in self.childs:
				hover = aChild.isHover(mousex,mousey,x+self.x,y+self.y,hover)
		return hover
			
	def setHover(self,child = None):
		self._hover = True
		if self.parent != None:
			self.parent.setHover()
			
	def _defaultonclic(self):
		return
			
	def onClic(self):
		self._onclic(self)
		
	def onRightClic(self):
		self._onrightclic(self)
	
	def onWheel(self):
		self._onclic(self)
		
	def onWheelUp(self):
		self._onrightclic(self)
			
	def onWheelDown(self):
		self._onrightclic(self)
	
	def _draw(self, x,y,alpha):
		if self.color != None:
			if alpha:
				bgl.glColor4f(self.color.r,self.color.g,self.color.b,self.color.a*self.getAlpha())
			else:
				bgl.glColor4f(*self.color.all)
			bgl.glBegin(bgl.GL_QUADS)
			bgl.glVertex2i(x, y)
			bgl.glVertex2i(x, y+self.h)
			bgl.glVertex2i(x+self.w, y+self.h)
			bgl.glVertex2i(x+self.w, y)
			bgl.glEnd()
			bgl.glColor4f(1.0,1.0,1.0,1.0)
		
			
	def draw(self, x,y,alpha):
		x = x+self.x
		y = y+self.y
		if self.anim[4] != None:
			if self.anim[4] >= 1.0:
				self.anim[4] = None
			elif self.anim[4] <= 0.0:
				self.visible = False
				self.anim[4] = None
			alpha = True
		if self._visibility and self.visible:
			self._draw(x,y,alpha)
		if self.visible:
			for aChild in self.childs:
				aChild.draw(x,y,alpha)
		
